package com.sap.jcp.sm.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.sap.jcp.sm.domain.Shift;

public class ShiftDAOImpl implements ShiftDAO {

	@SessionTarget
	Session session;
	@TransactionTarget
	Transaction transaction;

	@SuppressWarnings("unchecked")
	public List<Shift> listShift() {
		List<Shift> shiftList = null;
		try {
			//System.out.println("Listing shifts");
			shiftList = session.createQuery("from Shift").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return shiftList;
	}

	public void saveShift(Shift shift) {
		try {
			//System.out.println("Saving Shift");
			session.save(shift);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	
	public void updateShift(Shift shift) {
		try {
			//System.out.println("update shifts");
			session.update(shift);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	public void deleteShift(Shift shift) {
		try {
			//System.out.println("delete shift");
			session.delete(shift);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}

	


	public Shift getShift(Long shiftId) {
		
		String hql = "from Shift where shiftId = :shiftId";
		Query query = session.createQuery(hql);
		query.setLong("shiftId", shiftId);
		List<Shift> shiftList = query.list();
		if (shiftList != null && !shiftList.isEmpty()) {
			return shiftList.get(0);
		}
		return null;
	}
}
